// pages/detailsS/detailsS.js
const db = wx.cloud.database(); //初始化数据库
import {
  getTimeNow
} from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    a: '',
    datalist: [],
    imgs: [],
    bottom: '',
    say: '',
    showtext: true,
    saysNumber: 0,
    placeholder: '请输入内容',
    says: [],
    time: '',
  },
  // 展开全部评论
  showtext() {
    this.setData({
      showtext: !this.data.showtext
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('jiazai',options)

        const that = this
        db.collection("a").get()
        .then(res => {
            console.log("11111111",res.data[0].a)
          that.setData({
            a: res.data[0].a,
          })
        })

   // const that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log("跳转过来的数据为", data)
      that.setData({
        datalist: data.data,
        //time: that.diaplayTime(data.data.time)
        time: data.data.time
      })
    })

    // 获取数据库中的评论信息 
    db.collection('ershou').orderBy("sendtime", "desc").where({
      _id: wx.getStorageSync('ershouID')
    }).get({
      success: (res) => {
        this.setData({
          says: res.data[0].aaa,
        })
      },
    })

    // 获取数据库中的 ==> 评论条数
    db.collection('ershou').where({
      _id: wx.getStorageSync('ershouID')
    }).get({
      success: (res) => {
        //console.log("评论条数为",res.data[0].aaa.length) 
        this.setData({
          saysNumber: res.data[0].aaa.length,
        })
      }
    })

    
  },

  //回复评论
  replytext(e) {
    //console.log("评论的e为",e)
    //console.log(e.currentTarget.dataset.id)
    //console.log(e.currentTarget.dataset.name)

    const id = e.currentTarget.dataset.id; //点击评论的 id
    const name = e.currentTarget.dataset.name; //点击评论的 name

    this.setData({
      placeholder: '回复' + name + '：',
    })

  },
  //分享 

  // 点击预览图片
  look(e) {
    console.log('eee' + e)
    const that = this;
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: that.data.datalist.imgs,

    })
  },

  // 解决：底部input，键盘弹起上移
  // 输入聚焦
  foucus: function (e) {
    var that = this;
    that.setData({
      inputBottom: e.detail.height
    })
  },
  //失去聚焦
  blur: function (e) {
    var that = this;
    that.setData({
      inputBottom: 0
    })
  },

  // 获取评论内容
  getsay(e) {
    this.setData({
      say: e.detail.value,
      // _id:
    })
  },

  // 提交评论内容 入数据库
  send() {
    const that = this.data;
    //判断评论，不能为空
    if (!that.say) {
      wx.showToast({
        icon: 'none',
        title: '评论信息不能为空 ！',
      })
      return;
    }
    // 取出 跳转过来的 详情记录  _id
    const _id = wx.getStorageSync('ershouID')
    const say = that.say
    if (that.placeholder === '请输入内容') {
      this.setData({
        placeholder: ''
      })
    }
    const placeholder = that.placeholder
    const sendtime = getTimeNow()
    console.log("详情记录_id为", _id)

    wx.cloud.callFunction({
      name: 'getsayS',
      data: {
        _id: _id,
        say: placeholder + say,
        sendtime: sendtime,
        sayavatarUrl: wx.getStorageSync('key').avatarUrl,
        saynickName: wx.getStorageSync('key').nickName
      },
      success: (res) => {
        this.onLoad() // 发送后调用 刷新页面
        this.setData({
          say: '', // 清空输入框的内容
          placeholder: '请输入内容', // 恢复提示
        })
        wx.showToast({
          icon: "none",
          title: '评论成功',
        })
      },
    })

  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) { 
    const that = this
   // console.log('分享的详情',res)
   // console.log('分享的详情',res.target.dataset.info)

    return {
      title: that.data.datalist.title,
     // path: '/pages/index/index?index=0',
      path: '/pages/detailsS/detailsS?datalist=' + that.data.datalist,
      imageUrl: that.data.datalist.imgs[0],  
      success: function (res) {
          wx.showToast({
            title: "分享成功",
            icon: 'success',
            duration: 2000
          })
       },
      fail: function (res) {
      },
    }
  },



  // 分享  /* 必须为 button 组件才可以 */
/*    shareto() {
    let id = this.data.id;
    return {
      title: '参数',
      path: '/pages/detailsS/detailsS?index=' + 0 ,
      imageUrl: this.data.datalist.imgs[0],  
      success: function (res) {
          wx.showToast({
            title: "分享成功",
            icon: 'success',
            duration: 2000
          })
       },
      fail: function (res) {
      },
    }
    
  },  
 */




  // 时间 封装的时间方法 ，与数据库中保存的时间 相加减 则可得到 几分钟之前等数据
  diaplayTime: function (dateTimeStamp) {
    let newT = dateTimeStamp.replace(/-/g, '/'); //ios时间格式转换时，要求2019/07/04
    var result;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var curTime = new Date(newT); //后端返回的是秒数
    var diffValue = now - curTime;
    //console.log('dateTimeStamp',dateTimeStamp);
    //console.log('diffValue',diffValue);
    if (diffValue < 0) {
      return;
    }
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (monthC >= 1) {
      if (monthC <= 12)
        result = "" + parseInt(monthC) + "月前";
      else {
        result = "" + parseInt(monthC / 12) + "年前";
      }
    } else if (weekC >= 1) {
      result = "" + parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
      result = "" + parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
      result = "" + parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
      result = "" + parseInt(minC) + "分钟前";
    } else {
      result = "刚刚";
    }
    return result;
  },


/*   return() {
    wx.navigateBack({
      delta: 0,
    })
  }, */

})